package com.example.demo.controller;

import com.example.demo.domain.Order;
import com.example.demo.domain.RoomType;
import com.example.demo.domain.RoomTypeKey;
import com.example.demo.service.OrderService;
import com.example.demo.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RoomTypeService roomTypeService;

    //length用户要求产生字符串的长度
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    @RequestMapping
    public String OrderPages(Model model){
        List<Order> ordersBook = orderService.selectByStatus("预订");
        List<Order> ordersCompleted = orderService.selectByStatus("完成");
        List<Order> ordersConfirm = orderService.selectByStatus("确认");
        model.addAttribute("ordersBook",ordersBook);
        model.addAttribute("ordersCompleted",ordersCompleted);
        model.addAttribute("ordersConfirm",ordersConfirm);
        return "pages/order/orderList.html";
    }

    @GetMapping("/create")
    public String OrderCreatePages(Model model, @RequestParam("hotelid") String hotelid, @RequestParam("roomid") String roomid, @RequestParam("price") String price){
        model.addAttribute("price",price);
        RoomType roomType = roomTypeService.selectByPrimaryKey(new RoomTypeKey(hotelid, roomid));
        model.addAttribute("roomType",roomType);
        return "pages/order/orderCreate.html";
    }

    @PostMapping("/create")
    public String saveOrders(Order order,@RequestParam("price") String price){
        order.setOrdertime(new Date());
        String Randomorderid=getRandomString(10);
        order.setOrderid(Randomorderid);
        order.setUserid("005");
        order.setStatus("预订");
        order.setOrderamount(Float.valueOf(price)*order.getRoomnum());
        orderService.saveOrder(order);
        return "redirect:/order";
    }

    @RequestMapping("/search")//返回json
    @ResponseBody
    public List<Order> findOrder(){
        return orderService.findOrder();
    }

    @GetMapping("/view/{orderId}")
    public  String OrderViewPages(@PathVariable("orderId") String id,Model model){
        Order order=orderService.selectByPrimaryKey(id);
        model.addAttribute("order",order);

        String[] DateCheckIn = new SimpleDateFormat("yyyy-MM-dd").format(order.getTimecheckin()).toString().split("-");
        String[] DateCheckout = new SimpleDateFormat("yyyy-MM-dd").format(order.getTimecheckout()).toString().split("-");
        model.addAttribute("DateCheckIn",DateCheckIn);
        model.addAttribute("DateCheckout",DateCheckout);
        String hotelID=order.getHotelid();
        String roomId=order.getRoomid();
        RoomType roomType= roomTypeService.selectByPrimaryKey(new RoomTypeKey(hotelID,roomId));
        model.addAttribute("roomType",roomType);
        return "/pages/order/Orderview.html";
    }


}
